﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "GbmpModel.h"
#include "ISectionViewSymbolNew.h"
#include "DbDataSchema.h"
#include "RegenDataSchema.h"
#include "ElementId.h"
#include "IModelViewEventHandler.h"
#include "GbmpSectionViewInput.h"
#include "IElementDeletionComponent.h"

namespace gcmp
{
    class IElement;
    class IDocument;
    class IGraphicsElementShape;
    class IModelView;

    enum class SectionViewSymbolNewAuxiliaryType
    {
        None,
        BoxSectionPlanePointType,      //剖切盒 剖面的中心
        BoxLeftPlanePointType,         //剖切盒 左平面的中心
        BoxRightPlanePointType,        //剖切盒 右平面的中心
        BoxBottomPlanePointType,        //剖切盒 深度方向底平面的中心
        BoxUpPlanePointType,        //剖切盒 上平面的中心
        BoxDownPlanePointType,        //剖切盒 下平面的中心

        SymbolLeftPointType,        // （剖切盒上、下表面所在平面）标记符号左边控制点
        SymbolRightPointType,        // （剖切盒上、下表面所在平面）标记符号右边控制点
        SymbolUpPointType,          //（剖切盒左、右表面所在平面）标记符号上控制点
        SymbolDownPointType,        //（剖切盒左、右表面所在平面）标记符号下控制点

        //剖切盒除剖面、深度方向底平面（即前后平面）外的4个面的边（12条）可以由以下7条边投影得到：
        BoxUpPlaneLeftBorderType,      //剖切盒 上平面的左侧边
        BoxUpPlaneRightBorderType,     //剖切盒 上平面的右侧边
        BoxUpPlaneFrontBorderType,     //剖切盒 上平面的前侧边（剖面一侧）
        BoxUpPlaneBackBorderType,      //剖切盒 上平面的后侧边（底面一侧）
        BoxRightPlaneFrontBorderType,  //剖切盒 右平面的前侧边（剖面一侧）
        BoxRightPlaneBackBorderType,   //剖切盒 右平面的后侧边（底面一侧）
        BoxRightPlaneBottomBorderType, //剖切盒 右平面的下侧边（下表面一侧）
    };

    class GBMP_MODEL_EXPORT SectionViewSymbolNew : public NdbObjectSchematic, public ISectionViewSymbolNew
    {
        CANNOT_COPY_OR_ASSIGN(SectionViewSymbolNew)
    private:
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, SectionViewSymbolNew, gcmp::NdbObjectSchematic, 81F3B42E - 179F - 44FB - A1AE - 67B55DF40295, gbmpmodel, ISectionViewSymbolNew)
            DATA_TRANSIENT(IElement*, pOwnerElement)
            DATA(ElementId,SectionModelViewId) //剖面视图Id
            DATA_CUSTOM(Bool, MarkChanged,gcmp::PBT_Transient|gcmp::PBT_SupportCopy|gcmp::PBT_SupportTransaction) //为刷新视图设置的标记位
            DATA_WITHOUT_CHECK_AND_SUPPORT(OwnerPtr<IModelViewEventHandler>, ModelViewEventHandler)
            DATA_REGEN(Bool, ModelViewRangeUpdated)
            DATA(double,SymbolLeftPtOffset) // 标记符号左边控制点较视图原点位置偏移,视图原点右侧偏移为负
            DATA(double, SymbolRightPtOffset) // 标记符号右边控制点较视图原点位置偏移,视图原点左侧偏移为负
            DATA(double, SymbolUpPtOffset) // 标记符号上边控制点较视图原点位置偏移,视图原点下侧偏移为负
            DATA(double, SymbolDownPtOffset) // 标记符号下边控制点较视图原点位置偏移,视图原点上侧偏移为负
            DATA_REGEN(Coordinate3d,SectionViewCoord)  //视图的定位坐标系
            DBOBJECT_DATA_END

    public:
        virtual ~SectionViewSymbolNew() {}

#pragma region static methods
    public:
        static ISectionViewSymbolNew* Create(IDocument *pDoc, ElementId SectionModelViewId, const Coordinate3d& sectionViewCoord);

        static OwnerPtr<IGraphicsElementShape> CreateModelViewElementShape(const GbmpSectionViewInput& sectionViewInput, const IModelView* pModelView);

        static gcmp::UniIdentity GetImplementationUid();
#pragma endregion static methods

#pragma region IExternalObject 接口
    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual const IElement* GetOwnerElement() const override;
        virtual IElement* GetOwnerElement() override;

        virtual NdbObject* GetTopOwnerObject() const override;

        virtual void UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds) override;

        virtual void ReportParents(IElementParentReporter& reporter) const override;

        virtual bool FinishEditing() override;

#pragma endregion IExternalObject 接口

#pragma region ISectionViewSymbolNew 接口
    public:
        virtual ElementId GetModelViewId() const override;

        virtual ElementId GetOwnerElementId() const override;

#pragma endregion ISectionViewSymbolNew 接口

#pragma region 其他
    public:
        OwnerPtr<IGraphicsElementShape> CreateModelViewElementShape(const IModelView* pModelView) const;

        bool MakeTransform(const Matrix4d& matrix);

        void MarkChanged();

        IDocument* GetDocument() const;

        IModelView* GetSectionModelView() const;

        double GetSymbolLeftPtOffset() const;
        void SetSymbolLeftPtOffset(double offset);
        double GetSymbolRightPtOffset() const;
        void SetSymbolRightPtOffset(double offset);
        double GetSymbolUpPtOffset() const;
        void SetSymbolUpPtOffset(double offset);
        double GetSymbolDownPtOffset() const;
        void SetSymbolDownPtOffset(double offset);

        const Coordinate3d& GetSectionViewCoord() const;
        void SetSectionViewCoord(const Coordinate3d& sectionViewCoord);

#pragma endregion 其他

    private:
        void SetSectionModelViewId(const ElementId& viewId);
        bool SubscribeModelViewEvent();
        bool UnsubscribeModelViewEvent();
    };
}
